package com.ulook.lookplanet.util.mgc;


import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 描述：敏感词工具类, 用于操作敏感词库
 *
 * @author L1998
 * @date 2021/5/26
 */
public class SensitiveUtil {
    /**
     * 敏感词汇库
     */
    public static Map sensitiveWordMap;
    /**
     * 创建最小敏感词过滤类型
     */
    public static int minMatchType = 1;
    /**
     * 创建最大敏感词过滤类型,过滤所有
     */
    public static int maxMatchType = 2;

    /**
     * @return int
     * @Author MrKay
     * @Description 获取敏感词库敏感词数量
     * @Date 2020/5/12
     * @Param [sensitiveWordMap]
     */
    public static int getSensitiveWordsNumber() {
        if (sensitiveWordMap == null) {
            return 0;
        }
        return SensitiveUtil.sensitiveWordMap.size();
    }

    /**
     * @return boolean
     * @Author MrKay
     * @Description 判断是否包含敏感词
     * @Date 2020/5/12
     * @Param [txtString, matchTpe]
     */
    public static boolean isContainsSensitiveWord(String txtString, int matchTpe) {
        boolean flag = false;
        for (int i = 0; i < txtString.length(); i++) {
            //获取文本中包含敏感词的数量
            int sensitiveNum = getNumberFTxt(txtString, i, matchTpe);
            if (sensitiveNum > 0) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * @return java.util.Set<java.lang.String>
     * @Author MrKay
     * @Description 获取敏感词汇内容
     * @Date 2020/5/12
     * @Param [txtString, matchType]
     */
    public static Set<String> getSensitiveContext(String txtString, int matchType) {
        //初始化Set集合
        HashSet sensitiveSet = new HashSet();
        for (int i = 0; i < txtString.length(); i++) {
            //查询文本中敏感词数量
            int sensitiveNum = getNumberFTxt(txtString, i, matchType);
            if (sensitiveNum > 0) {
                sensitiveSet.add(txtString.substring(i, i + sensitiveNum));
                //移动循环角标
                i = i + sensitiveNum - 1;
            }
        }
        return sensitiveSet;
    }

    /**
     * @return java.lang.String
     * @Author MrKay
     * @Description 替换文本中的敏感词
     * @Date 2020/5/13
     * @Param [txtString, matchType, replaceStr]
     */
    public static String replaceSensitiveWord(String txtString, int matchType, String replaceChar) {
        String resultTxt = txtString;
        //获取敏感词内容
        Set<String> set = getSensitiveContext(txtString, matchType);
        Iterator<String> iterator = set.iterator();
        String word = null;
        String replaceString = null;
        while (iterator.hasNext()) {
            word = iterator.next();
            replaceString = getReplaceString(replaceChar, word.length());
            resultTxt = resultTxt.replaceAll(word, replaceString);
        }
        return resultTxt;
    }

    /**
     * @return java.lang.String
     * @Author MrKay
     * @Description 将敏感词汇替换为指定字符
     * @Date 2020/5/13
     * @Param [replaceChar, length]
     */
    public static String getReplaceString(String replaceChar, int length) {
        String replaceString = replaceChar;
        for (int i = 0; i < length; i++) {
            replaceString += replaceChar;
        }
        return replaceString;
    }

    /**
     * @return int
     * @Author MrKay
     * @Description 获取文本中敏感词数量
     * @Date 2020/5/12
     * @Param [txtString, matchTpe]
     */
    public static int getNumberFTxt(String txtString, int beginIndex, int matchTpe) {
        boolean flag = false;
        //初始化敏感词汇数量
        int sensitiveNum = 0;
        //初始化文本中字符
        char word = 0;
        Map nowMap = SensitiveUtil.sensitiveWordMap;
        for (int i = beginIndex; i < txtString.length(); i++) {
            word = txtString.charAt(i);
            nowMap = (Map) nowMap.get(word);
            if (nowMap != null) {
                sensitiveNum++;
                //判断是否已经到了敏感词的结尾,是的话通过matchType判断是否需要继续检测
                if ("1".equals(nowMap.get("isEnd"))) {
                    flag = true;
                    if (SensitiveUtil.minMatchType == matchTpe) {
                        break;
                    }
                }
            } else {//为空就跳出
                break;
            }
        }
        if (!flag) {
            sensitiveNum = 0;
        }
        return sensitiveNum;
    }
}
